package com.portme.client.pages.widgets;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RichTextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.portme.client.PortmeServiceClient;
import com.portme.client.StaticFactory;
import com.portme.client.interfaces.Observer;
import com.portme.client.pages.richtext.RichTextToolbar;
import com.portme.shared.ClientPost;

public class PostDisplayWidget extends VerticalPanel implements Observer{
	private final String STYLE_NAME = "-single-post";
	private String theme;
	private Long currentPostId;
	private ClientPost currentPost;
	private Long tabId;
	
	private TextBox postTitleBox = new TextBox();
	private RichTextArea postRichTextBox = new RichTextArea();
	
	private Label title;
	private HTML htmlText;
	
	
	//Click handlers
	private ClickHandler publishEditPostHandler = new ClickHandler() {
		
		@Override
		public void onClick(ClickEvent event) {
			if(!postTitleBox.getText().isEmpty() && currentPostId != null) {
				PortmeServiceClient.getInstance().editPost(currentPostId, postTitleBox.getText(), postRichTextBox.getHTML(), new AsyncCallback<ClientPost>() {
					
					@Override
					public void onSuccess(ClientPost result) {
						showPost(result);
					}
					
					@Override
					public void onFailure(Throwable caught) {
						// TODO Auto-generated method stub
						Window.alert("Could not create post!");
					}
				});
			} else {
				Window.alert("Title must be set!");
			}
		}
	};
	
	private ClickHandler discardPostHandler = new ClickHandler() {
		
		@Override
		public void onClick(ClickEvent event) {
//			PostDisplayWidget.this.clear();
			showPost(currentPost);
		}
	};
	
	public PostDisplayWidget() {
		this.theme = StaticFactory.getInstance().getTheme();
		addStyleName(this.theme+STYLE_NAME);
		StaticFactory.getInstance().observeTheme(this);
	}
	
	public void setTabId(Long tab) {
		this.tabId = tab;
	}
	
	public void setCurrentPost(Long post) {
		this.currentPostId = post;
	}
	
	private void setPost(ClientPost post) {
		this.currentPost = post;
	}
	
	public void showPost(final ClientPost post) {
		this.setCurrentPost(post.getId());
		this.setPost(post);
		this.clear();
		
		HorizontalPanel titlebar = new HorizontalPanel();
//		titlebar.setSpacing(10);
		title = new Label(post.getTitle());
		titlebar.add(title);
		if(StaticFactory.getInstance().allowPermission()) {
			Button editPost = new Button("Edit", new ClickHandler() {
				
				@Override
				public void onClick(ClickEvent event) {
					// TODO Auto-generated method stub
					editPost(post);
				}
			});
			editPost.getElement().setAttribute("style", "margin-right:10px;");
			
			titlebar.add(editPost);
			
			Button deletePost = new Button("Delete", new ClickHandler() {
				
				@Override
				public void onClick(ClickEvent event) {
					// TODO Auto-generated method stub
					if(Window.confirm("Are you sure?"))
						deletePost(post.getId());
				}
			});
			
			titlebar.add(deletePost);
		}
		
		title.addStyleName(this.theme+STYLE_NAME+"-title");
		this.add(titlebar);
//		this.add(new HTML(post.getText()));
		htmlText = new HTML(post.getText());
		htmlText.addStyleName(this.theme+STYLE_NAME+"-text");
		htmlText.setWordWrap(true);
		this.add(htmlText);
		Window.setTitle(post.getTitle());
	}

	public void editPost(ClientPost post) {
		this.clear();
		this.add(new Label("Title : "));
		postTitleBox.setWidth("99%");
		postTitleBox.setText(post.getTitle());
		this.add(postTitleBox);
		this.add(new Label("Body : "));
		
		//RichTextArea postRichTextBox = new RichTextArea();
		postRichTextBox.setHTML(post.getText());
	    postRichTextBox.ensureDebugId("cwRichText-area");
	    postRichTextBox.setSize("100%", "14em");
	    RichTextToolbar toolbar = new RichTextToolbar(postRichTextBox);
	    toolbar.ensureDebugId("cwRichText-toolbar");
	    toolbar.setWidth("100%");

	    // Add the components to a panel
	    Grid grid = new Grid(2, 1);
	    grid.setStyleName("cw-RichText");
	    grid.setWidget(0, 0, toolbar);
	    grid.setWidget(1, 0, postRichTextBox);
		
		this.add(grid);
		HorizontalPanel buttons = new HorizontalPanel();
		buttons.setSpacing(5);
		buttons.add(new Button("Save Changes", publishEditPostHandler));
		buttons.add(new Button("Cancel", discardPostHandler));
		this.add(buttons);
	}
	
	private void deletePost(Long postId) {
		PortmeServiceClient.getInstance().deletePost(postId, new AsyncCallback<Boolean>() {

			@Override
			public void onFailure(Throwable caught) {
				Window.alert("Cannot delete post");
			}

			@Override
			public void onSuccess(Boolean result) {
				if(result) {
					History.newItem(""+StaticFactory.getInstance().getCurrentProfile().getUser().split("@")[0]+"/"+tabId, true);
				}
			}
		});
	}
	
	@Override
	public void notifyMe(Object arg) {
		String newTheme = (String)arg;
		if(newTheme != null) {
			removeStyleName(this.theme+STYLE_NAME);
//			title.removeStyleName(this.theme+STYLE_NAME+"-title");
//			htmlText.removeStyleName(this.theme+STYLE_NAME+"-text");

			this.theme = newTheme;
			
			addStyleName(newTheme+STYLE_NAME);
//			title.addStyleName(this.theme+STYLE_NAME+"-title");
//			htmlText.removeStyleName(this.theme+STYLE_NAME+"-text");
		}
	}

}
